//options
clear all
set maxvar  32000, permanently
set matsize 11000, permanently
set more off, permanently
global bootstraps 1000
set seed 23

// macros
global klmshare				: env klmshare
global projects				: env projects
global klmperry             : env klmperry
global storageb				: env storageb

global perrydatas       = "${klmperry}/CBA/data/perry/raw"
global perrydata        = "${klmperry}/CBA/data/perry/clean"
global masterinter  	= "${klmshare}/CurrentRAs/FredB/test"
global output           = "${projects}/ece_parenting/tex_files/other"
global dataperry        = "$klmperry/PerryPreschool/Data/Perry_PARI_and_Other_Data/PARI/DATA_PARI/"
global abcjpeanalysis   = "${klmshare}/Data_Central/Abecedarian/data/ABC-CARE/extensions/cba-iv/"
global dataihdp         = "${projects}/ece_parenting/data"
global dataihdp         = "$storageb/dc_data/"

cd  $dataihdp
use ihdp_data, clear
rename _all, lower

replace   bw = bw/1000
destring bwg , replace 

// impute iq
replace   stndscor =  iqcage     if stndscor ==.
replace   iqcage   =  stndscor   if iqcage ==.
foreach var of varlist stndscor iqcage { 
	replace `var' = mdi_24cor    if stndscor ==. & iqcage ==.
}
replace   stndscor =  iqcage     if stndscor ==.
replace   iqcage   =  stndscor   if iqcage ==.

// list relevant variables
global baseline_child      twin sex bw anga black hispanic 
global baseline_mother     mage meduc works married 
global baseline_household  welfare tot_siblings_natural employed_adult
global baseline_economy    employment medinc gpc
global outputs             iqcage stndscor 
global inputs              cum_avg_daycare_36m_sum

// mark baseline sample
reg  $baseline_child $baseline_mother $baseline_household $baseline_economy $outputs $inputs
gen  sample1 = e(sample) 
keep if sample1 == 1

// parenting latents, ages 1 and 3
foreach var of varlist alt_subscale_1_12-alt_subscale_6_12 alt_subscale_1_36-alt_subscale_8_36 {
	summ `var'
	gen  `var'_std = (`var' - r(mean))/r(sd)
}
# delimit
sem (_cons@0 X -> alt_subscale_1_12_std) (_cons@0 X -> alt_subscale_2_12_std) (_cons@0 X -> alt_subscale_3_12_std) 
	(_cons@0 X -> alt_subscale_4_12_std) (_cons@0 X -> alt_subscale_5_12_std) (_cons@0 X -> alt_subscale_6_12_std), method(adf);
predict parenting_age1, latent;
sem (_cons@0 X -> alt_subscale_1_36_std) (_cons@0 X -> alt_subscale_2_36_std) (_cons@0 X -> alt_subscale_3_36_std) (_cons@0 X -> alt_subscale_4_36_std)
	(_cons@0 X -> alt_subscale_5_36_std) (_cons@0 X -> alt_subscale_6_36_std) (_cons@0 X -> alt_subscale_7_36_std) (_cons@0 X -> alt_subscale_8_36_std), 
																																						 cov(e.alt_subscale_1_36_std*e.alt_subscale_2_36_std)
																																						 cov(e.alt_subscale_3_36_std*e.alt_subscale_4_36_std)
																																						 cov(e.alt_subscale_5_36_std*e.alt_subscale_6_36_std)
																																						 cov(e.alt_subscale_7_36_std*e.alt_subscale_8_36_std)
																																						 cov(e.alt_subscale_4_36_std*e.alt_subscale_7_36_std)
																																						 cov(e.alt_subscale_2_36_std*e.alt_subscale_7_36_std) method(adf);		
 predict parenting_age3, latent;
# delimit cr
egen    parenting_ages13 = rowmean(parenting_age1 parenting_age3)

// standardize latents in sample
foreach var of varlist parenting_ages13 {
	summ    `var' if tg == 0
	gen     `var'_std = (`var' - r(mean))/r(sd)
	replace `var'_std = `var'_std + 100
}
// log and standardize childcare variable 
summ    cum_avg_daycare_36m_sum  if tg == 0 
replace cum_avg_daycare_36m_sum  = (cum_avg_daycare_36m_sum    - r(mean))/r(sd)
replace cum_avg_daycare_36m_sum  =  cum_avg_daycare_36m_sum    + r(mean)

global inputs_std cum_avg_daycare_36m_sum parenting_ages13_std
keep   ihdp site tg bwg $baseline_child $baseline_mother $baseline_household $baseline_economy $inputs_std $outputs alt_subscale*

rename cum_avg_daycare_36m_sum c
rename parenting_ages13_std    p


// age 1
factor alt_subscale_1_12 alt_subscale_2_12 alt_subscale_3_12 alt_subscale_4_12 alt_subscale_5_12 alt_subscale_6_12
matrix eigen = e(Ev)
matrix loadings = e(L)

forvalues i=1/6{
local eigen`i'=eigen[1,`i']
	forvalues j=1/3{
		local loading_`j'_`i'=loadings[`i',`j']
	}
local eigen`i':di %6.2f `eigen`i''
local loading_1_`i':di %6.2f `loading_1_`i''
local loading_2_`i':di %6.2f `loading_2_`i''
local loading_3_`i':di %6.2f `loading_3_`i''
}

cd $output
texdoc init factor_table_12m.tex, replace force
tex \begin{tabular}[tl]{lccccccccc} 
tex \noalign{\hrule height 1.5pt} 
tex \textbf{\textit{Panel a.} Age One} &  & \makecell[l]{Subscale:} &\makecell[tc]{Parental \\ Warmth} &\makecell[tc]{Parental\\Verbal\\Skills}&
tex \makecell[tc]{Parental\\Lack of \\Hostility}&\makecell[tc]{Learning/\\Literacy}&\makecell[tc]{Activities/\\Outgoings}&
tex \makecell[tc]{Developmental\\Advance}& \\
tex \hdashline
tex \multicolumn{2}{l}{\textbf{\textit{Panel a1.} Brief Description}}  & &  &&&  &&&\\
tex Number of Items & & & 7 & 3 & 5 & 7 & 3 & 4 &\\
tex \addlinespace
tex Example of an Item &  && \makecell[l]{Parent \\ responds positively \\ to praise of child \\ offered by visitor}& \makecell[l]{Parent \\ converses \\ freely  \\ and easily} & 
tex \makecell[l]{Parent \\ does not scold \\ or criticize child \\ during visit} & \makecell[l]{Toys for \\ literature \\ and \\ music} &  
tex \makecell[l]{Child gets \\ out of house \\ at least four \\ times/week}	&
tex \makecell[l]{Parent \\ structures \\ child's \\ play periods} &\\
tex \hdashline
tex \multicolumn{2}{l}{\textbf{\textit{Panel a2.} Factor Analysis}}   & & &&&&&&\\
tex  \multicolumn{1}{c}{Factor}	&	& Eigenvalue & \multicolumn{6}{c}{Loadings}& \\
tex \cmidrule(lr){1-1} \cmidrule(lr){3-3} \cmidrule(lr){4-10}
tex \multicolumn{1}{c}{1} && \textbf{`eigen1'} & \textbf{`loading_1_1'} &\textbf{`loading_1_2'}&\textbf{`loading_1_3'}&\textbf{`loading_1_4'}&\textbf{`loading_1_5'}&\textbf{`loading_1_6'} &\\
tex \multicolumn{1}{c}{2} & & `eigen2' & `loading_2_1' &`loading_2_2'&`loading_2_3'&`loading_2_4'&`loading_2_5'&`loading_2_6'&\\
tex \multicolumn{1}{c}{3} & & `eigen3' & `loading_3_1' &`loading_3_2'&`loading_3_3'&`loading_3_4'&`loading_3_5'&`loading_3_6'&\\
tex \noalign{\hrule height 1.5pt} 
tex \end{tabular}
texdoc close

// age 3
factor alt_subscale_1_36 alt_subscale_2_36 alt_subscale_3_36 alt_subscale_4_36 alt_subscale_5_36 alt_subscale_6_36 alt_subscale_7_36 alt_subscale_8_36
matrix eigen = e(Ev)
matrix loadings = e(L)

forvalues i = 1/8{
local eigen`i' = eigen[1,`i']
	forvalues j = 1/4{
		local loading_`j'_`i' = loadings[`i',`j']
	}
local eigen`i':di %6.2f `eigen`i''
local loading_1_`i':di %6.2f `loading_1_`i''
local loading_2_`i':di %6.2f `loading_2_`i''
local loading_3_`i':di %6.2f `loading_3_`i''
local loading_4_`i':di %6.2f `loading_4_`i''
}

texdoc init factor_table_36m.tex, replace force
tex \begin{tabular}[t]{lccc ccccccc c} 
tex \noalign{\hrule height 1.5pt} 
tex  \textbf{\textit{Panel b.} Age Three} & &\makecell[tl]{Subscale:} & \makecell[tc]{Learning\\Stimu- \\lation} &\makecell[tc]{Access\\to\\Reading}&
tex \makecell[tc]{Parental\\Verbal\\Skills}&\makecell[tc]{Parental\\Warmth}&\makecell[tc]{Home\\Exterior}&
tex \makecell[tc]{Home\\Interior}&\makecell[tc]{Outings/\\Activities}& \makecell[tc]{Parental\\Lack of \\Hostility}& \\
tex \hdashline
tex \multicolumn{2}{l}{\textbf{\textit{Panel b1.} Brief Description}}   &  &&&  &&& & &\\
tex Number of Items & & & 14 & 5 & 2 & 9 & 3 & 4 & 3 & 3 &\\
tex \addlinespace
tex Example of an Item &  && \makecell[l]{Child has \\ three \\ or more \\ puzzles} & \makecell[l]{Child has \\ access to \\ at least 10 \\ children's \\ books} & 
tex \makecell[l]{Parent uses\\ complex \\ sentence \\ structure  and \\ vocabulary} & \makecell[l]{Parent holds\\ child close \\10–15 min \\ per day} & 
tex  \makecell[l]{Building \\ appears \\ safe}& \makecell[l]{Rooms are \\not \\overcrowded \\with \\furniture} & 
tex \makecell[l]{Child has \\ been taken \\ to a museum \\ during the \\ past year}& \makecell[l]{Parent \\ does not use \\ physical \\ restraint \\ during visit} &\\
tex \hdashline
tex \multicolumn{2}{l}{\textbf{\textit{Panel b2.} Factor Analysis}}  & &  &&&&&& & &\\
tex  \multicolumn{1}{c}{Factor}	&	& Eigenvalue & \multicolumn{8}{c}{Loadings}& \\
tex \cmidrule(lr){1-1} \cmidrule(lr){3-3} \cmidrule(lr){4-12}
tex \multicolumn{1}{c}{1} && \textbf{`eigen1'} & \textbf{`loading_1_1'} &\textbf{`loading_1_2'}&\textbf{`loading_1_3'}&\textbf{`loading_1_4'}&\textbf{`loading_1_5'}&
tex \textbf{`loading_1_6'}& \textbf{`loading_1_7'}&\textbf{`loading_1_8'}&\\

tex \multicolumn{1}{c}{2} && `eigen2' & `loading_2_1' &`loading_2_2'&`loading_2_3'&`loading_2_4'&`loading_2_5'&`loading_2_6'&`loading_2_7'&`loading_2_8'&\\
tex \multicolumn{1}{c}{3} && `eigen3' & `loading_3_1' &`loading_3_2'&`loading_3_3'&`loading_3_4'&`loading_3_5'&`loading_3_6'&`loading_3_7'&`loading_3_8'&\\
tex \noalign{\hrule height 1.5pt} 
tex \end{tabular}
texdoc close
